package pers.qianyu.month_202012.date_20201213;

import org.junit.*;

/**
 * 413. 等差数列划分
 * https://leetcode-cn.com/problems/arithmetic-slices/
 *
 * @author mizzle rain
 * @date 2020-12-13 20:49
 */
public class NumberOfArithmeticSlices {
    public int numberOfArithmeticSlices(int[] A) {
        int len = A.length;
        if (len < 3) {
            return 0;
        }
        // dp[i] 表示以 A[i] 结尾的等差序列个数
        int[] dp = new int[len];
        for (int i = 2; i < len; i++) {
            if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
                dp[i] = 1 + dp[i - 1];
            }
        }
        int res = 0;
        for (int d : dp) {
            res += d;
        }
        return res;
    }

    @Test
    public void test1() {
        int result = new NumberOfArithmeticSlices()
                .numberOfArithmeticSlices(new int[]{1, 2, 3, 4});
        System.out.println("result = " + result);
    }
}
